//done
package Server;

import java.awt.Color;
import java.awt.Rectangle;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import java.awt.SystemColor;
import java.awt.Font;

/**
* @author ameer
* @author waleed
* @author bshara
* @author kamil
* 
*/

public class ServerLoginWindow extends JFrame
{
	/**
	 * JLabel of the port
	 */
	private JLabel portLabel = null;
	/**
	 * JLabel of MySQL Password
	 */
	private JLabel  my_SQLpasswordLabel = null;
	/**
	 * JLabel of MySQL User Name
	 */
	private JLabel  my_SQLusernamedLabel = null;
	/**
	 * JTextField if failed on port
	 */
	private JTextField portLabelField = null;
	/**
	 * JTextField if failed on User name
	 */
	private JTextField my_SQLusernameField = null;
	/**
	 * JPasswordField if failed on password
	 */
	private JPasswordField my_SQLpasswordField = null;
	/**
	 * JButton of start button
	 */
	private JButton startButton = null;
	/**
	 * JPanel of the window
	 */
	private JPanel jContentPane = null;
	/**
	 * JTextField of DB name
	 */
	private JTextField MysqlDbNametextField;
	/**
	 * JLabel of the DB name
	 */	
	private JLabel lblMysqlDbName;
	
	/**
	 * This is the default constructor
	 */
	public ServerLoginWindow() 
	{
		super();
		initialize();		
	}
	
	/**
	 * This method initializes ServerInitialize
	 */
	private void initialize()
	{
		this.setSize(600, 480);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setContentPane(getJContentPane());
		this.setTitle("CCRM Client Connect");		
	}
	/**
	 * This method initializes LoginForm
	 */
	private JPanel getJContentPane() 
	{
		if (jContentPane == null) 
		{
			jContentPane = new JPanel();
			jContentPane.setBackground(SystemColor.desktop);
			jContentPane.setLayout(null);
			
			JLabel lblWelcomeToCcrm = new JLabel("Welcome to CCRM System");
			lblWelcomeToCcrm.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblWelcomeToCcrm.setForeground(new Color(255, 0, 0));
			lblWelcomeToCcrm.setBounds(218, 24, 205, 58);
			jContentPane.add(lblWelcomeToCcrm);
			
			portLabel  = new JLabel();
			portLabel .setBounds(new Rectangle(298, 168, 113, 22));
			portLabel .setText("MySQL Port:");
			
			my_SQLusernamedLabel = new JLabel();
			my_SQLusernamedLabel.setBounds(new Rectangle(24, 120, 113, 22));
			my_SQLusernamedLabel.setText("MySQL user name:");
			
			my_SQLpasswordLabel = new JLabel();
			my_SQLpasswordLabel.setBounds(new Rectangle(298, 120, 113, 22));
			my_SQLpasswordLabel.setText("MySQL password:");
			
			jContentPane.add(portLabel, null);
			jContentPane.add(my_SQLpasswordLabel, null);
			jContentPane.add(my_SQLusernamedLabel, null);
			jContentPane.add(getPortLabelField(), null);
			jContentPane.add(getMy_SQLusernameField(), null);
			jContentPane.add(getMy_SQLpasswordField(), null);
			
			lblMysqlDbName = new JLabel();
			lblMysqlDbName.setText("MySQL DB Name:");
			lblMysqlDbName.setBounds(new Rectangle(293, 80, 113, 22));
			lblMysqlDbName.setBounds(24, 168, 113, 22);
			jContentPane.add(lblMysqlDbName);
			
			MysqlDbNametextField = new JTextField();
			MysqlDbNametextField.setBounds(new Rectangle(389, 80, 130, 22));
			MysqlDbNametextField.setBounds(137, 168, 130, 22);
			jContentPane.add(MysqlDbNametextField);
			jContentPane.add(getStartButton(),null);
			
			JLabel label_1 = new JLabel("");
			label_1.setIcon(new ImageIcon(ServerStartWindow.class.getResource("/Pictures/server.png")));
			label_1.setBounds(253, 213, 158, 102);
			jContentPane.add(label_1);
			
			JLabel label = new JLabel("");
			label.setIcon(new ImageIcon(ServerStartWindow.class.getResource("/Pictures/bg.png")));
			label.setBounds(0, 0, 600, 480);
			jContentPane.add(label);
		}
		return jContentPane;		
	}
	
	/**
	 * This method initializes portLabelField	
	 */
	private JTextField getPortLabelField()
	{
		if (portLabelField == null) 
		{
			portLabelField = new JTextField();
			portLabelField.setBounds(new Rectangle(411, 168, 130, 22));
		}
		return portLabelField;
	}
	
	
	/**
	 * This method initializes my_SQLusernameField	
	 */
	private JTextField getMy_SQLusernameField()
	{
		if (my_SQLusernameField == null) 
		{
			my_SQLusernameField = new JTextField();
			my_SQLusernameField.setBounds(new Rectangle(137, 120, 130, 22));
			my_SQLusernameField.setText("");
		}
		return my_SQLusernameField;
	}
	
	/**
	 * This method initializes my_SQLpasswordField	
	 */
	private JPasswordField getMy_SQLpasswordField()
	{
		if (my_SQLpasswordField == null) 
		{
			my_SQLpasswordField = new JPasswordField();
			my_SQLpasswordField.setBounds(new Rectangle(411, 120, 130, 22));
			my_SQLpasswordField.setText("");
		}
		return my_SQLpasswordField;
	}
	
	/**
	 * This method initializes startButton	
	 */
	public JButton getStartButton() 
	{
		
		if (startButton == null) 
		{
			startButton = new JButton();
			startButton.setBounds(new Rectangle(222, 326, 135, 29));
			startButton.setText("START");
		}	
		return startButton;
	}	
	
	/**
	 *  This method return the entered port	MySQL userName
	 */
	public String getMySQLuserName()
	{
		if (my_SQLusernameField != null)
			return my_SQLusernameField.getText();
		return null;		
	}
	
	/**
	 *  This method return the entered MySQL password	
	 */
	public char[] getMySQLpassword()
	{
		if (my_SQLpasswordField != null)
			return my_SQLpasswordField.getPassword();
		return null; 		
	}
	
	/**
	 *  This method return the entered port	
	 */
	public int getPort()
	{
		int port=0;
		String msg;
		try
		{
			if (portLabelField != null)
			{
				msg = portLabelField.getText();
				if (msg.equals(""))
					return 0;
				port = Integer.parseInt(msg);
			}	
			return port;
		}catch (Exception e)
		{
			return -1;
		}		
	}
	
	/**
	 *  This method return the entered MySQL DataBase Name	
	 */
	public String getMySQL_DB_Name(){
		String DB_Name = null;
		try{
			if (MysqlDbNametextField != null){
				DB_Name = MysqlDbNametextField.getText();
				if (DB_Name.equals(""))
					return null;
			}	
			return DB_Name;
		}
		catch (Exception e){
			return null;
		}		
	}
}
